{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# UMAPartitioner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import set_env"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tvm\n",
    "import tvm.relay as relay\n",
    "from tvm.relay.backend.contrib.uma import uma_available\n",
    "from tvm.relay.backend.contrib.uma.api import UMAPartitioner\n",
    "from tvm.relay.op.contrib.register import get_pattern_table\n",
    "from tvm.relay.testing import mlp, resnet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试 partition_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "partitioner = UMAPartitioner(\"test_partition\")\n",
    "assert get_pattern_table(\"test_partition\") is None\n",
    "\n",
    "partitioner.register()\n",
    "\n",
    "assert get_pattern_table(\"test_partition\") is not None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_pattern_table(\"test_partition\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "for workload, backend, merge in [\n",
    "    (\"resnet\", \"dnnl\", False),\n",
    "    (\"resnet\", \"dnnl\", True),\n",
    "    (\"mlp\", \"dnnl\", False),\n",
    "    (\"mlp\", \"dnnl\", True),\n",
    "    (\"resnet\", \"cutlass\", False),\n",
    "    (\"resnet\", \"cutlass\", True),\n",
    "    (\"mlp\", \"cutlass\", False),\n",
    "    (\"mlp\", \"cutlass\", True),\n",
    "]:\n",
    "    partitioner = UMAPartitioner(backend, merge)\n",
    "    pattern_table = get_pattern_table(backend)\n",
    "    # print(pattern_table)\n",
    "    for entry in pattern_table:\n",
    "        partitioner.add_pattern(*entry)\n",
    "\n",
    "    if workload == \"resnet\":\n",
    "        net = resnet.get_net(1, 10)\n",
    "    elif workload == \"mlp\":\n",
    "        net = mlp.get_net(1, 10)\n",
    "    else:\n",
    "        assert False, f\"don't know how to find workload for {workload}\"\n",
    "\n",
    "    mod = tvm.ir.IRModule()\n",
    "    mod[\"main\"] = net\n",
    "\n",
    "    partitioner.register()\n",
    "    partitioned_mod = partitioner.partition(mod)\n",
    "\n",
    "    def partition_default(mod):\n",
    "        \"\"\"partitions using default BYOC flow\"\"\"\n",
    "\n",
    "        sequence = [\n",
    "            relay.transform.MergeComposite(pattern_table),\n",
    "            relay.transform.AnnotateTarget(backend),\n",
    "        ]\n",
    "\n",
    "        if merge:\n",
    "            sequence.append(relay.transform.MergeCompilerRegions())\n",
    "\n",
    "        sequence.append(relay.transform.PartitionGraph())\n",
    "        sequential = tvm.transform.Sequential(sequence)\n",
    "\n",
    "        return sequential(mod)\n",
    "\n",
    "    default_partitioned_mod = partition_default(mod)\n",
    "\n",
    "    assert len(partitioned_mod.functions) == len(default_partitioned_mod.functions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py312x",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
